window.onload = function() {
  var oMenu = document.querySelector("#menu"),
    List = document.querySelector("#list"),
    Lists = List.children,
    menus = document.querySelector("#menu").children,
    menuLength = menus.length,
    listLength = Lists.length,
    node = document.querySelector(".node");
    // tip = document.querySelector("#tip");

  //全局文件夹名称
  var dirname = null;
  var  thisDir = null;
  for (i = 0; i < listLength; i++) {
    Lists[i].addEventListener(
      "mouseenter",
      function(e) {
        // if (this === tip) {
        //   dirname = null;
        //   return;
        // }
        dirname = this.children[1];
      },
      false
    );
  }
  //鼠标右键菜单
  function showMenu(e) {
    oMenu.style.height = 120 + "px";
    //取消默认的浏览器自带右键 很重要！！
    e.preventDefault();
    //获取我们自定义的右键菜单
    var menu = document.querySelector("#menu");
    //根据事件对象中鼠标点击的位置，进行定位
    menu.style.left = e.clientX + "px";
    menu.style.top = e.clientY + "px";
    //改变自定义菜单的宽，让它显示出来
    menu.style.width = "125px";
  }

  window.oncontextmenu = function(e) {
    showMenu(e);
  };

  //关闭右键菜单
  window.onclick = function(e) {
    oMenu.style.height = 0;
  };
  $("#createAlbum").click(function(){
    menus[0].click()
  })

  //右键菜单点击逻辑处理
  for (let i = 0; i < menuLength; i++) {
    menus[i].addEventListener("click", function() {
      if (i == 0) {
        // tip.style.display = "none";
        List.appendChild(node);
        var input = document.querySelector("#input");
        input.focus();
        input.addEventListener("keydown", function(e) {
          if (e.keyCode == 13) {
            input.blur();
          }
        });
        input.addEventListener("blur", function() {
          if (this.value == "") {
            List.removeChild(node);
          } else {
            mkdir(this.value);
          }
        });
      } else if (i == 1) {
        if (dirname === null) {
          alert("请选择相册！");
          return;
        }
        for (var j = 0; j < listLength; j++) {
          thisDir = Lists[j].children[1];
          if (thisDir.innerText == dirname.innerText) {
            let oldName = thisDir.innerText;
            thisDir.innerHTML =
              "<input type='text' id='new' value=" + dirname.innerText + "  />";
            var domNew = document.querySelector("#new");
            domNew.focus();
            domNew.select();
            domNew.addEventListener("keydown", function(e) {
              if (e.keyCode == 13) {
                this.blur();
              }
            });
            domNew.addEventListener("blur", function() {
              if (oldName === this.value) {
                domNew.parentNode.innerText = this.value;
                return;
              }
              renamedir(oldName, this.value, domNew);
            });
          }
        }
      } else if (i == 2) {
        if (dirname === null) {
          alert("请选择相册！");
          return;
        }
        show_confirm(dirname);
      }
    });
  }
};

//创建相册ajax请求
function mkdir(value) {
  $.post("/createdir", { dirname: value }, function(data) {
    if (data.code === 0) {
      window.location.reload();
    } else if (data.code === 1010) {
      alert("相册已存在，创建相册失败！");
      window.location.reload();
    } else {
      alert("创建相册失败！");
      window.location.reload();
    }
  });
  document.querySelector("#text-node").innerHTML = "<h4>" + value + "<h4>";
}

//重命名相册ajax请求
function renamedir(oldName, newName, domNew) {
  if (oldName == "") {
    alert("请选择您要重命名的相册");
    return false;
  }
  $.post("/renamedir", { oldname: oldName, newname: newName }, function(data) {
    if (data.code == 1010) {
      alert("相册已存在，重命名失败！");
      domNew.parentNode.innerHTML = oldName;
    } else {
      domNew.parentNode.innerHTML = newName;
    }
  });
}

//删除相册ajax请求
function removeDir(dirname) {
  var val = dirname.innerText;
  $.post("/removedir", { dirname: val }, function(data) {
    if (data.code == 0) {
      //dirname.parentNode.style.display = "none";
      window.location.reload();
    }
  });
}

//确认删除
function show_confirm(dirname) {
  var r = confirm("确定删除？");
  if (r == true) {
    removeDir(dirname);
  } else {
    return false;
  }
}
